<?php

namespace app\model;

use think\Model;

class RechargeLimitTemplateVideo extends Model
{
	// 设置当前模型对应的数据表
	protected $name = 'recharge_limit_template_video';

	public function rechargeLimitTemplate()
	{
		return $this->hasOne(RechargeLimitTemplate::class, 'id', 'recharge_limit_template_id');
	}

	public function deleteByVideoId(int $videoId)
	{
		return $this->where(['video_id' => $videoId])->delete();
	}

	public function selectWithTemplateFirstByVideoId(array $fields, int $videoId)
	{
		$data = $this->with(['rechargeLimitTemplate' => function ($query) {
			$query->withField(['id', 'recharge_name',  'recharge_day', 'recharge_amount', 'recharge_set']);
		}])->field($fields)->where(['video_id' => $videoId])->find();

		return empty($data) ? [] : $data->toArray();
	}

	// 根据条件获取数据
    public function getDataByWhere($where = [], $fields = 'video_id')
    {
        $data = $this::where($where)->field($fields)->select();
        return $data ? $data->toArray() : [];
    }
}
